const enterpriseVue = new Vue({
  el: '#app',
  data() {
    const Parent_Number = (rule, value, callback) => {
      if (this.form.Level != '1' && !value) {
        callback(new Error('当前菜单等级不是一级菜单，请选择父级菜单'));
      } else {
        callback();
      }
    }
    const Url = (rule, value, callback) => {
      if (this.children == '否' && !value) {
        callback(new Error('当前菜单不含子集，请输入菜单路由'));
      } else {
        callback();
      }
    }
    const Icon = (rule, value, callback) => {
      if (this.form.Level == '1' && !value) {
        callback(new Error('当前菜单是一级菜单，请选的菜单图标'));
      } else {
        callback();
      }
    }
    return {
      rules: {
        Model_Name: [{required: true, message: '请选择菜单名称', trigger: 'blur'}],
        Level:[{ required: true, message: '请选择菜单等级', trigger: 'blur'}],
        Parent_Number:[{required:true, validator:Parent_Number , trigger:'change'}],
        Url:[{required:true,validator:Url,trigger: 'blur'}],
        Icon:[{required:true,validator:Icon,trigger: 'change'}]
      },
      form: {
        Model_Number: '',
        Model_Name: '',
        Parent_Number: '',
        Level: '',
        Url: '',
        Path: '',
        Icon: '',
        Order: '',
        Children:{
          system_model_mark:[]
        }
      },
      Level: [{
        label: '一级菜单',
        value: '1'
      }, {
        label: '二级菜单',
        value: '2'
      }, {
        label: '三级菜单',
        value: '3'
      }],
      Role:[{
        label:'全部',
        value:'0'
      },{
        label:'仅系统管理员',
        value:'1'
      }],
      Icon:[],
      Parent: [],
      children: '否',
      loading: false,
      isParent: true,
      isUrl:false
    }
  },
  mounted() {
    this.getIcon()
  },
  methods: {
    getIcon(){
      const _this = this;
      $.ajax({
        url: '../../../js/Icon.json',
        type: 'GET',
        success: function (data) {
          _this.Icon = data
        },
      })
    },
    getMenu(e) {
      if (e != '1') {
        const _this = this;
        Level =  e-1+''
        $.ajax({
          url: DomainName + '/getLevelMenuAction?Level='+Level,
          type: 'GET',
          success: function (res) {
            _this.Parent = res
            _this.isParent = false
          },
          error: function () {
            _this.$message.error("操作失败，请稍后再试")
          },
          complete: function (XMLHttpRequest) {
            if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
              window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
            }
          }
        })
      }else{
        this.Parent = []
        this.isParent = true
        this.form.Parent_Number = ''
        this.children = '是'
        this.getUrl(this.children)
      }
    },
    getUrl(e){
      if(e === '是'){
        this.isUrl = true
        this.form.Url = ''
        this.form.Children.system_model_mark = []
      }else if(e === '否'){
        this.isUrl = false
        this.form.Children.system_model_mark = []
      }
    },
    submitForm(formName) {
      let _this = this
      // console.log(_this.form);
      this.$refs[formName].validate((valid) => {
        if (valid) {
          _this.loading = true
          $.ajax({
            url: `${DomainName}/getAddAction?token=${$.cookie('token')}`,
            type: 'POST',
            data: {
              Data: JSON.stringify({
                system_model_list: [_this.form]
              }),
            },
            success: function (res) {
              if (res.Status === 'success') {
                _this.$message.success("提交成功")
                window.history.go(-1);
              } else {
                _this.$message.error("提交失败，请稍后再试")
              }
            },
            error: function () {
              _this.$message.error("操作失败，请稍后再试")
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    add_system_model_mark(){
      this.form.Children.system_model_mark.push({
        Mark_Number: '',
        Model_Number: '',
        Mark_Name: ''
      })
      
    },
    remove_system_model_mark(){
      if (this.form.Children.system_model_mark.length>0) {
        this.form.Children.system_model_mark.pop()
      }else{
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  }
})